package com.cmlite.base;

import java.sql.SQLException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;

import com.cmlite.A1002Login;
import com.cmlite.R;
import com.cmlite.database.DBManager;

public abstract class BaseActivity extends Activity implements IActivity {

	protected DBManager mDB;
	protected Context mContext;
	protected Resources mRes;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mContext = this;
		mRes = this.getResources();
		this.onIOnCreate(savedInstanceState);
		boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
		if (tabletSize) {
			// do something
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
		} else {
			// do something else
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		}
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		this.onIStart();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		this.onIRestart();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		this.onIActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		this.onIBackPressed();
		if (this instanceof A1002Login) {
			finish();
		}
	}

	protected void openDatabase() {
		if (mDB == null) {
			mDB = new DBManager(this);
		}
		try {
			mDB.open();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		this.onIResume();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		this.onIDestroy();
		if (mDB != null) {
			mDB.close();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (mContext instanceof A1002Login) {
				finish();
				return true;
			}
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void callBackCancel(String email) {

	}

	public void callBackAccept(String email) {

	}

	public boolean isTablet() {
		try {
			// Compute screen size
			DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
			float screenWidth = dm.widthPixels / dm.xdpi;
			float screenHeight = dm.heightPixels / dm.ydpi;
			double size = Math.sqrt(Math.pow(screenWidth, 2)
					+ Math.pow(screenHeight, 2));
			// Tablet devices should have a screen size greater than 6 inches
			return size >= 6;
		} catch (Throwable t) {
			Log.e("BaseActivity", "Failed to compute screen size", t);
			return false;
		}
	}
}
